/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_xml_element.h
 * @date:   Mar 11, 2012
 */

#ifndef INSTALLER_UTIL_XML_U_ELEMENT_H_
#define INSTALLER_UTIL_XML_U_ELEMENT_H_

#include <stdio.h>
#include <stdbool.h>

#include "xml_element.h"

#include "u_main.h"

#ifdef __cplusplus
extern "C" {
#endif

bool u_installer_xml_element_start();          /* unit entry point */

bool u_installer_xml_element_new();
bool u_installer_xml_element_delete();
bool u_installer_xml_element_add_attribute();
bool u_installer_xml_element_add_element();
bool u_installer_xml_element_get_attribute();
bool u_installer_xml_element_get_element(); 
bool u_installer_xml_element_get_parent();
bool u_installer_xml_element_get_name();
bool u_installer_xml_element_get_value();
bool u_installer_xml_element_set_parent();
bool u_installer_xml_element_set_name();
bool u_installer_xml_element_set_value();

bool u_installer_xml_element_start() {
    return (u_installer_xml_element_new()
        && u_installer_xml_element_delete()
    	&& u_installer_xml_element_add_attribute()
    	&& u_installer_xml_element_add_element()
    	&& u_installer_xml_element_get_attribute()
    	&& u_installer_xml_element_get_element()
    	&& u_installer_xml_element_get_parent()
        && u_installer_xml_element_get_name()
        && u_installer_xml_element_get_value()
        && u_installer_xml_element_set_parent()
        && u_installer_xml_element_set_name()
        && u_installer_xml_element_set_value());
}

bool u_installer_xml_element_new() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");

    if (element == NULL) {
        u_installer_main_print("xml_element", "new()", false);

        return false;
    }
    
    u_installer_main_print("xml_element", "new()", true);

    installer_xml_element_delete(element);

    return true;
}

bool u_installer_xml_element_delete() {
    /* unit case is a part of new() */        
    u_installer_main_print("xml_element", "delete()", true);

    return true;
}

bool u_installer_xml_element_add_attribute() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");
    
    XMLAttribute *attribute1 = installer_xml_attribute_new(L"attr1", L"val1");
    XMLAttribute *attribute2 = installer_xml_attribute_new(L"attr2", L"val2");
    XMLAttribute *attribute3 = installer_xml_attribute_new(L"attr3", L"val3");

    if ((element == NULL) || (attribute1 == NULL) || (attribute2 == NULL) 
	    || (attribute3 == NULL)) {
        u_installer_main_print("xml_element", "add_attribute(1)", false);

        return false;
    }
    
    /* element shall have no attributes by default */
    if (element->attributes != NULL) {
        u_installer_main_print("xml_element", "add_attribute(2)", false);
	
	    return false;
    }
    
    installer_xml_element_add_attribute(element, attribute1);
    if ((wcscmp(installer_xml_attribute_get_name(element->attributes), L"attr1") != 0)
        || (wcscmp(installer_xml_attribute_get_value(element->attributes), L"val1") != 0)) {
		
	    u_installer_main_print("xml_element", "add_attribute(3)", false);
	
	    return false;
    }
    
    /* add same element to the list */
    installer_xml_element_add_attribute(element, attribute1);
    if ((wcscmp(installer_xml_attribute_get_name(element->attributes), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes), L"val1") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next), L"val1") != 0)) {
		
	    u_installer_main_print("xml_element", "add_attribute(4)", false);
	
	    return false;
    }

    installer_xml_element_add_attribute(element, attribute2);
    if ((wcscmp(installer_xml_attribute_get_name(element->attributes), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes), L"val1") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next), L"val1") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next->next), L"attr2") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next->next), L"val2") != 0)) {
		
	    u_installer_main_print("xml_element", "add_attribute(5)", false);
	
	    return false;
    }
    
    installer_xml_element_add_attribute(element, attribute3);
    if ((wcscmp(installer_xml_attribute_get_name(element->attributes), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes), L"val1") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next), L"attr1") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next), L"val1") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next->next), L"attr2") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next->next), L"val2") != 0)
	        || (wcscmp(installer_xml_attribute_get_name(element->attributes->next->next->next), L"attr3") != 0)
	        || (wcscmp(installer_xml_attribute_get_value(element->attributes->next->next->next), L"val3") != 0)) {
		
	    u_installer_main_print("xml_element", "add_attribute(6)", false);
	
	    return false;
    }
        
    u_installer_main_print("xml_element", "add_attribute()", true);
    
    return true;
}

bool u_installer_xml_element_add_element() {
    XMLElement *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement *element2 = installer_xml_element_new(L"elem2", L"val2");
    XMLElement *element3 = installer_xml_element_new(L"elem3", L"val3");
    XMLElement *element4 = installer_xml_element_new(L"elem4", L"val4");
    
    XMLElement *unit_element2 = NULL;
    XMLElement *unit_element3 = NULL;
    XMLElement *unit_element4 = NULL;
    
    if ((element1 == NULL) || (element2 == NULL) || (element3 == NULL) 
                || (element4 == NULL)) {
        u_installer_main_print("xml_element", "add_element(1)", false);

        return false;
    }
    
    /*
     *          Element1
     *         /        \
     * Element2          Element3
     *    |
     * Element4
     */
    
    /* the real pointer to the element2 */
    unit_element2 = installer_xml_element_add_element(element1, element2);
    unit_element3 = installer_xml_element_add_element(element1, element3);
    unit_element4 = installer_xml_element_add_element(unit_element2, element4);
    
    /* directly going through the XMLElement structure */
    if ((wcscmp(installer_xml_element_get_name(element1->elements), L"elem2") != 0)
            || (wcscmp(installer_xml_element_get_name(element1->elements->next), L"elem3") != 0)
            || (element1->elements->next->next != NULL)
            || (wcscmp(installer_xml_element_get_name(element1->elements->elements), L"elem4") != 0)) {
            
        u_installer_main_print("xml_element", "add_element(2)", false);
	
	    return false;
    }
    
    installer_xml_element_delete(element1);
    installer_xml_element_delete(element2);
    installer_xml_element_delete(element3);
    installer_xml_element_delete(element4);
    installer_xml_element_delete(unit_element2);
    installer_xml_element_delete(unit_element3);
    installer_xml_element_delete(unit_element4);
    
    u_installer_main_print("xml_element", "add_element()", true);

    return true;
}

bool u_installer_xml_element_get_attribute() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");
    
    /*
     * attribute2 have key same as attribute1
     * attribute3 have value same as value 2
     */
    XMLAttribute *attribute1 = installer_xml_attribute_new(L"attr1", L"val1");
    XMLAttribute *attribute2 = installer_xml_attribute_new(L"attr1", L"val2");
    XMLAttribute *attribute3 = installer_xml_attribute_new(L"attr3", L"val2");
    
    XMLAttribute *unit = NULL;

    if ((element == NULL) || (attribute1 == NULL) || (attribute2 == NULL) 
	    || (attribute3 == NULL)) {
        u_installer_main_print("xml_element", "get_attribute(1)", false);

        return false;
    }
    
    installer_xml_element_add_attribute(element, attribute1);
    installer_xml_element_add_attribute(element, attribute2);
    installer_xml_element_add_attribute(element, attribute2);
    installer_xml_element_add_attribute(element, attribute3);
    
    unit = installer_xml_element_get_attribute(element, NULL);
    if ((wcscmp(installer_xml_attribute_get_name(unit), L"attr1") != 0)
        || (wcscmp(installer_xml_attribute_get_value(unit), L"val1") != 0)) {
		
	    u_installer_main_print("xml_element", "get_attribute(2)", false);
	
	    return false;
    }
    
    unit = installer_xml_element_get_attribute(element, unit);
    if ((wcscmp(installer_xml_attribute_get_name(unit), L"attr1") != 0)
        || (wcscmp(installer_xml_attribute_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_element", "get_attribute(3)", false);
	
	    return false;
    }
    
    unit = installer_xml_element_get_attribute(element, unit);
    if ((wcscmp(installer_xml_attribute_get_name(unit), L"attr1") != 0)
        || (wcscmp(installer_xml_attribute_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_element", "get_attribute(4)", false);
	
	    return false;
    }
    
    unit = installer_xml_element_get_attribute(element, unit);
    if ((wcscmp(installer_xml_attribute_get_name(unit), L"attr3") != 0)
        || (wcscmp(installer_xml_attribute_get_value(unit), L"val2") != 0)) {
		
	    u_installer_main_print("xml_element", "get_attribute(5)", false);
	
	    return false;
    }
    
    unit = installer_xml_element_get_attribute(element, unit);
    if (unit != NULL) {
        u_installer_main_print("xml_element", "get_attribute(6)", false);
	
	    return false;
    }
    
    u_installer_main_print("xml_element", "get_attribute()", true);
    
    return true;
}

bool u_installer_xml_element_get_element() {
    XMLElement *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement *element2 = installer_xml_element_new(L"elem2", L"val2");
    XMLElement *element3 = installer_xml_element_new(L"elem3", L"val3");
    XMLElement *element4 = installer_xml_element_new(L"elem4", L"val4");
    
    XMLElement *unit_element2 = NULL;
    XMLElement *unit_element3 = NULL;
    XMLElement *unit_element4 = NULL;
    
    XMLElement *unit;
    
    if ((element1 == NULL) || (element2 == NULL) || (element3 == NULL) 
                || (element4 == NULL)) {
        u_installer_main_print("xml_element", "get_element(1)", false);

        return false;
    }
    
    /*
     *          Element1
     *         /        \
     * Element2          Element3
     *    |
     * Element4
     */
    
    /* the real pointer to the element2 */
    unit_element2 = installer_xml_element_add_element(element1, element2);
    unit_element3 = installer_xml_element_add_element(element1, element3);
    unit_element4 = installer_xml_element_add_element(unit_element2, element4);
    
    /* Element1 -> Element2 */
    unit = installer_xml_element_get_element(element1, NULL);
    if (wcscmp(installer_xml_element_get_name(unit), L"elem2") != 0) {    
        u_installer_main_print("xml_element", "get_element(2)", false);
	    return false;
    }
    
    /* Element1 -> Element3 */
    unit = installer_xml_element_get_element(element1, unit);
    if (wcscmp(installer_xml_element_get_name(unit), L"elem3") != 0) {
        u_installer_main_print("xml_element", "get_element(3)", false);
	    return false;
    }
    
    /* Element1 -> NULL */
    unit = installer_xml_element_get_element(element1, unit);
    if (unit != NULL) {
        u_installer_main_print("xml_element", "get_element(4)", false);
	    return false;
    }
    
    /* Element2 -> Element4 */
    unit = installer_xml_element_get_element(unit_element2, unit);
    if (wcscmp(installer_xml_element_get_name(unit), L"elem4") != 0) {
        u_installer_main_print("xml_element", "get_element(5)", false);
	    return false;
    }
    
    /* Element2 -> NULL */
    unit = installer_xml_element_get_element(unit_element2, unit);
    if (unit != NULL) {
        u_installer_main_print("xml_element", "get_element(6)", false);
	    return false;
    }
    
    installer_xml_element_delete(element1);
    installer_xml_element_delete(element2);
    installer_xml_element_delete(element3);
    installer_xml_element_delete(element4);
    installer_xml_element_delete(unit_element2);
    installer_xml_element_delete(unit_element3);
    installer_xml_element_delete(unit_element4);
    
    u_installer_main_print("xml_element", "get_element()", true);

    return true;
}

bool u_installer_xml_element_get_parent() {
    XMLElement  *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement  *element2 = installer_xml_element_new(L"elem2", L"val2");
    XMLElement  *element3 = installer_xml_element_new(L"elem3", L"val3");
    
    if ((element1 == NULL) || (element2 == NULL) || (element3 == NULL)) {
        u_installer_main_print("xml_element", "get_parent(1)", false);

        return false;
    }

    element2->parent = element1;
    element3->parent = element2;
    
    if ((installer_xml_element_get_parent(element1) != NULL)
            || (installer_xml_element_get_parent(element2) != element1)
            || (installer_xml_element_get_parent(element3) != element2)) {
        u_installer_main_print("xml_element", "get_parent(2)", false);
        
        return false;
    }
    
    u_installer_main_print("xml_element", "get_parent()", true);
    
    installer_xml_element_delete(element1);
    installer_xml_element_delete(element2);
    installer_xml_element_delete(element3);
    
    return true;
}


bool u_installer_xml_element_get_name() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");

    if (element == NULL) {
        u_installer_main_print("xml_element", "get_name()", false);

        return false;
    }

    if (wcscmp(installer_xml_element_get_name(element), L"elem1") != 0) {
        u_installer_main_print("xml_element", "get_name()", false);
    
        return false;
    }
    
    u_installer_main_print("xml_element", "get_name()", true);

    installer_xml_element_delete(element);

    return true;
}

bool u_installer_xml_element_get_value() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");

    if (element == NULL) {
        u_installer_main_print("xml_element", "get_value()", false);

        return false;
    }

    if (wcscmp(installer_xml_element_get_value(element), L"val1") != 0) {
        u_installer_main_print("xml_element", "get_value()", false);

        return false;
    }
    
    u_installer_main_print("xml_element", "get_value()", true);

    installer_xml_element_delete(element);

    return true;
}

bool u_installer_xml_element_set_parent() {
    XMLElement *element1 = installer_xml_element_new(L"elem1", L"val1");
    XMLElement *element2 = installer_xml_element_new(L"elem2", L"val2");

    if ((element1 == NULL) || (element2 == NULL)) {
        u_installer_main_print("xml_element", "set_parent(1)", false);

        return false;
    }

    /*
     * Element1
     *    |
     * Element2
     */
    installer_xml_element_set_parent(element2, element1);

    if (installer_xml_element_get_parent(element2) != element1) {
        u_installer_main_print("xml_element", "set_parent(2)", false);

        return false;
    }
    
    u_installer_main_print("xml_element", "set_parent()", true);

    installer_xml_element_delete(element1);
    installer_xml_element_delete(element2);

    return true;
}

bool u_installer_xml_element_set_name() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");

    if (element == NULL) {
        u_installer_main_print("xml_element", "set_name()", false);

        return false;
    }

    installer_xml_element_set_name(element, L"elem2");

    if (wcscmp(installer_xml_element_get_name(element), L"elem2") != 0) {
        u_installer_main_print("xml_element", "set_name()", false);

        return false;
    }
    
    u_installer_main_print("xml_element", "set_name()", true);

    installer_xml_element_delete(element);

    return true;
}

bool u_installer_xml_element_set_value() {
    XMLElement *element = installer_xml_element_new(L"elem1", L"val1");

    if (element == NULL) {
        u_installer_main_print("xml_element", "set_value()", false);

        return false;
    }

    installer_xml_element_set_value(element, L"val2");

    if (wcscmp(installer_xml_element_get_value(element), L"val2") != 0) {
        u_installer_main_print("xml_element", "set_value()", false);

        return false;
    }
    
    u_installer_main_print("xml_element", "set_value()", true);

    installer_xml_element_delete(element);

    return true;
}


#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_XML_U_ELEMENT_H_ */ 
